package com.lyncs.ods.modules.txn.service.impl;

import com.lyncs.ods.common.exception.ApiException;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.txn.model.DeliverBillInfo;
import com.lyncs.ods.modules.txn.model.DeliverInfo;
import com.lyncs.ods.modules.txn.mapper.DeliverInfoMapper;
import com.lyncs.ods.modules.txn.service.DeliverBillInfoService;
import com.lyncs.ods.modules.txn.service.DeliverInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 交付单总表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@Service
public class DeliverInfoServiceImpl extends ServiceImpl<DeliverInfoMapper, DeliverInfo> implements DeliverInfoService {

    @Autowired
    private DeliverBillInfoService deliverBillInfoService;

    @Override
    public DeliverInfo getOneById(Long id) {
        DeliverInfo deliverInfo = this.lambdaQuery().eq(DeliverInfo::getId, id).one();
        if (Objects.isNull(deliverInfo) || LyncsOdsConstant.DeliverStatus.DELETED.getKey().equals(deliverInfo.getStatus())) {
            throw new ApiException("交付单不存在或已删除，ID：" + id);
        }
        return deliverInfo;
    }

    @Override
    public void updateDeliveryStatus(List<Long> deliverIds) {
        List<DeliverBillInfo> deliverBillInfos = deliverBillInfoService.lambdaQuery()
                .in(DeliverBillInfo::getDeliverId, deliverIds)
                .ne(DeliverBillInfo::getStatus, LyncsOdsConstant.DeliverStatus.DELETED.getKey())
                .list();
        Map<Long, List<Integer>> deliverBillStatusMap = deliverBillInfos.stream().collect(Collectors.groupingBy(DeliverBillInfo::getDeliverId, Collectors.mapping(DeliverBillInfo::getStatus, Collectors.toList())));
        deliverBillStatusMap.forEach((deliverId, status) -> {
            List<Integer> deliverBillStatus = status.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
            Integer finalStatus = deliverBillStatus.size() == 1 && LyncsOdsConstant.DeliverStatus.INIT.getKey().equals(deliverBillStatus.get(0)) ? LyncsOdsConstant.DeliverStatus.INIT.getKey() :
                    deliverBillStatus.size() == 1 && LyncsOdsConstant.DeliverStatus.SETTLED.getKey().equals(deliverBillStatus.get(0)) ? LyncsOdsConstant.DeliverStatus.SETTLED.getKey() :
                            LyncsOdsConstant.DeliverStatus.PARTIAL_SETTLEMENT.getKey();
            this.lambdaUpdate().set(DeliverInfo::getStatus,finalStatus).eq(DeliverInfo::getId,deliverId).update();
        });
        /*List<DeliverInfo> updateDeliverInfos = deliverBillStatusMap.entrySet().stream().map(entry -> {
            List<Integer> deliverBillStatus = entry.getValue().stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
            Integer status = deliverBillStatus.size() == 1 && LyncsOdsConstant.DeliverStatus.INIT.getKey().equals(deliverBillStatus.get(0)) ? LyncsOdsConstant.DeliverStatus.INIT.getKey() :
                    deliverBillStatus.size() == 1 && LyncsOdsConstant.DeliverStatus.SETTLED.getKey().equals(deliverBillStatus.get(0)) ? LyncsOdsConstant.DeliverStatus.SETTLED.getKey() :
                            LyncsOdsConstant.DeliverStatus.PARTIAL_SETTLEMENT.getKey();
            return new DeliverInfo().setId(entry.getKey()).setStatus(status);
        }).collect(Collectors.toList());
        this.updateBatchById(updateDeliverInfos);*/

    }
}
